package com.hongyuan.sellcoach.ui.main.view_mode;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellcoach.base.CustomViewModel;
import com.hongyuan.sellcoach.data.Constants;
import com.hongyuan.sellcoach.data.MyRepository;
import com.hongyuan.sellcoach.data.md5.BaseUtil;
import com.hongyuan.sellcoach.ui.main.beans.PhoneMessageTokenBean;
import com.hongyuan.sellcoach.utils.HourMeterUtil;

public class RetrievePasswordViewModel extends CustomViewModel<MyRepository> {

    private HourMeterUtil meterUtil;

    private int nowSecond = 60;

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //验证码的绑定
    public ObservableField<String> verificationCode = new ObservableField<>("");
    //验证码发生的文字描述
    public ObservableField<String> sendCodeText = new ObservableField<>("发送验证码");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
        //提示已修改成功
        public SingleLiveEvent<Boolean> showSuccess = new SingleLiveEvent<>();
    }

    public RetrievePasswordViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });

    //验证码发送点击
    public BindingCommand sendCode = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            if(nowSecond == 60){
                if(!BaseUtil.isValue(userName.get())){
                    ToastUtils.showShort("请输入手机号");
                    return;
                }
                setParams("m_mobile", userName.get());
                requestData(Constants.GET_MESSAGE_TOKEN);
            }

        }
    });

    //提交密码修改
    public BindingCommand submit = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            updatePassword();
        }
    });


    @Override
    public void onCreate() {
        super.onCreate();

        meterUtil = new HourMeterUtil();
        meterUtil.setTimeCallBack(passedTime -> {
            if(passedTime > 0 && passedTime < 60){
                nowSecond = 60-passedTime;

                sendCodeText.set((60-passedTime)+"s");
            }else{
                if(passedTime >= 60){
                    nowSecond = 60;
                    meterUtil.stopCount();
                    sendCodeText.set("发送验证码");
                }
            }
        });
    }

    /*
    * 修改密码请求
    * */
    private void updatePassword(){
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }

        if (TextUtils.isEmpty(verificationCode.get())) {
            ToastUtils.showShort("请输入验证码！");
            return;
        }

        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入新密码！");
            return;
        }

        clearParams().setParams("m_mobile", userName.get())
                .setParams("m_pwd",password.get())
                .setParams("re_pwd",password.get())
                .setParams("dxm",verificationCode.get());

        //修改密码
        requestData(Constants.DO_FIND_PASSWORD);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MESSAGE_TOKEN){
            PhoneMessageTokenBean tokenBean = (PhoneMessageTokenBean)dataBean;
            //开始倒计时
            meterUtil.startCount();

            //获取短信验证码
            setParams("m_mobile",userName.get()).setParams("sms_checked_code",tokenBean.getToken());
            requestData(Constants.SEND_REGI_MSG);
        }

        if(code == Constants.DO_FIND_PASSWORD){
            uc.showSuccess.call();
        }
    }
}
